AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:SpawnFunction(ply, tr)
	local tr = util.QuickTrace(ply:GetShootPos(), ply:GetAimVector() * 150, ply)
	if !tr.HitWorld then
		ply:ChatPrint("Look at the ground to spawn a turret.")
	else
		local SpawnPos = tr.HitPos + tr.HitNormal * 20
		local SpawnAng = tr.HitNormal:Angle()
		local ent = ents.Create("struct_turret")
		ent.dt.Owner = ply
		ent:SetPos(SpawnPos + (tr.HitNormal *-3))
		ent:SetAngles(SpawnAng + Angle(90, 0, 0))
		ent:Spawn()
		ent:Activate()
		local head = ents.Create( "auto_turret_gun" )
		head:SetPos( SpawnPos + (tr.HitNormal*18) )
		head:SetAngles( SpawnAng + Angle(90, 0, 0) )
		head:Spawn()
		head:Activate()
		head:SetParent(ent)
		head.Body = ent
		ent.Head = head
		head:SetOwner(ply)
		ent:SetNWEntity( "owner", ply )
	end
	
	return ent
end

function ENT:Initialize()
	self.BaseClass.Initialize(self)
	self:GetPhysicsObject():EnableMotion(false)
	self:SetNWBool("ison", false)
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(1000)
		phys:EnableMotion(false)
	end
	local ply = self.Entity:GetNWEntity( "owner" )
	self.Entity:SetNWBool("ison", false)
	self.Build = 50
	self.LastUsed = CurTime()
	Sound("ambient/energy/spark1.wav")
	Sound("ambient/energy/spark2.wav")
	Sound("ambient/energy/spark3.wav")
	Sound("ambient/energy/spark4.wav")
	self.Entity:SetNWInt("power",0)
	self.scrap = false
end

function ENT:Explode()
	local turretpos = self:GetPos()
	self.BaseClass.Explode(self)
	
	Notify(self.dt.Owner, 1, 3, "One of your sentry turrets has been destroyed!")
	
	if math.random(0, 10) >= 6 then
		local gun = ents.Create("weapon_turretgun")
		gun:SetPos(turretpos)
		gun:SetAngles(gun:GetAngles() + Angle(0, 0, 5)) //it bothers me that this would stand up straight
		gun:Spawn()
	end
end


function ENT:Use(ply, caller)
	if (self.Build<=0) then
		timer.Create( tostring(self.Entity) .. "toggler", 0.2, 1, self.ToggleOn, self, ply)
	end
	if self.LastUsed>CurTime() then return end
	if (self.Build>0) then 
		if (self.LastUsed+0.3<CurTime()) then
			self.LastUsed = CurTime()-0.1
		end
		self.LastUsed = CurTime()+0.1
		
		if ply:GetTable().Tooled then
			self.Build = self.Build-3
			self.Entity:EmitSound("ambient/energy/spark" .. math.random(1,4) .. ".wav")
		else
			self.Build = self.Build-1
			if (self.Build%3==0) then
				self.Entity:EmitSound("ambient/energy/spark" .. math.random(1,4) .. ".wav")
			end
		end
		if (self.Build<=0) then
			self.dt.Health = self.dt.Health + 225
			Notify( ply, 0, 3, "Sentry turret built.")
			self.Pickup = false
		end
	end
end

function ENT:ToggleOn(ply)
	if (ply!=self.Entity:GetNWEntity("owner")) then
		Notify( ply, 4, 3, "This is not your turret!")
	else
		self.Entity:SetNWBool("ison", !self.Entity:GetNWBool("ison"))
		if (self.Entity:GetNWBool("ison")==false) then
			Notify( ply, 0, 3, "Sentry turret turned OFF" );
		else
			Notify( ply, 0, 3, "Sentry turret turned ON" );
		end
	end
end

function ENT:IsBuilt()
	if (self.Build<=0) then return true else return false end
end